<?php

namespace app\index\service;

use app\common\model\AccountBill;
use app\common\model\Trade;
use app\index\dao\MemberDao;
use app\index\model\UM as UM;

/**
 * Class InviteRewardService
 * @package app\index\service
 */
class InviteRewardService
{
    // dao
    private MemberDao $memberDao;

    public function __construct(MemberDao $memberDao)
    {
        $this->memberDao = $memberDao;
    }

    /**
     * 列表
     * @param UM $user
     * @return array
     */
    public function read(UM $user): array
    {
        $inviteCoinTime = $user->inviteCoinTime;
        $inviteCardTime = $user->inviteCardTime;
        $memberId = $user->id;

        // 邀请赠送获客卡
        $card = AccountBill::where('toMemberId', $memberId)
            ->where('toType', 'member')
            ->where('created', '>', $inviteCardTime)
            ->where('billType', TRANSFER_CARD_INVITE)
            ->where('accountType', PAYMENT_CARD)
            ->field(['id'])
            ->find();

        // 被邀请人充值后，邀请人获得奖励
        $coin = AccountBill::where('toMemberId', $memberId)
            ->where('toType', 'member')
            ->where('created', '>', $inviteCoinTime)
            ->where('billType', TRANSFER_SHARE_INVITER)
            ->field(['id'])
            ->find();

        return [
            'card' => $card ? 1 : 0,
            'coin' => $coin ? 1 : 0,
        ];
    }

}
